import "package:ct_pa_majordomo/pages/transfer_station.dart";
import "package:ct_pa_majordomo/pages/user/api.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import 'package:flutter/material.dart';
import 'package:ct_pa_majordomo/widgets/header.dart';
import 'package:ct_pa_majordomo/widgets/button/index.dart';
import 'package:ct_pa_majordomo/config/theme_vars.dart';
import "package:flutter_easyloading/flutter_easyloading.dart";

import "../../../utils/storage.dart";

class ModifyPasswordPage extends StatefulWidget {
  const ModifyPasswordPage({Key? key}) : super(key: key);

  @override
  State<ModifyPasswordPage> createState() => _ModifyPasswordPageState();
}

class _ModifyPasswordPageState extends State<ModifyPasswordPage> {
  final _formKey = GlobalKey<FormState>();
  final TextEditingController _oldPwdController = TextEditingController();
  final TextEditingController _newPwdController = TextEditingController();
  final TextEditingController _confirmPwdController = TextEditingController();
  bool _isLoading = false;

  void _submit() async {
    if (_formKey.currentState?.validate() ?? false) {
      setState(() {
        _isLoading = true;
      });
      EasyLoading.show();
      // TODO: 调用修改密码接口
      print(_newPwdController.text);
      Map<String, dynamic> data = {
        "newPassword": _newPwdController.text,
        "oldPassword": _oldPwdController.text,
      };
      try {
        await userApi.updatePwd(data);
      } catch(e) {
        print(e);
      }
      EasyLoading.dismiss();
      setState(() {
        _isLoading = false;
      });
      if (!mounted) return;
      EasyLoading.showSuccess("密码修改成功");
      _logout(context);
    }
  }

  Future<void> _logout(BuildContext context) async {
    storage.remove("token");
    storage.remove("apartment");
    storage.remove("info");
    Stacker.pushAndRemoveUntil(const TransferStation());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const Header(title: Text('修改密码')),
      body: Padding(
        padding: const EdgeInsets.all(ThemeVars.paddingPage),
        child: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                padding: const EdgeInsets.all(20),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.05),
                      blurRadius: 10,
                      offset: const Offset(0, 4),
                    ),
                  ],
                ),
                child: Form(
                  key: _formKey,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      TextFormField(
                        controller: _oldPwdController,
                        obscureText: true,
                        decoration: const InputDecoration(
                          labelText: '原始密码',
                          border: OutlineInputBorder(),
                          isDense: true,
                          contentPadding: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
                        ),
                        style: const TextStyle(fontSize: 15),
                        validator: (value) {
                          if (value == null || value.isEmpty) {
                            return '请输入原始密码';
                          }
                          return null;
                        },
                      ),
                      const SizedBox(height: 16),
                      TextFormField(
                        controller: _newPwdController,
                        obscureText: true,
                        decoration: const InputDecoration(
                          labelText: '新密码',
                          border: OutlineInputBorder(),
                          isDense: true,
                          contentPadding: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
                        ),
                        style: const TextStyle(fontSize: 15),
                        validator: (value) {
                          if (value == null || value.isEmpty) {
                            return '请输入新密码';
                          }
                          if (value.length < 6) {
                            return '新密码长度不能少于6位';
                          }
                          return null;
                        },
                      ),
                      const SizedBox(height: 16),
                      TextFormField(
                        controller: _confirmPwdController,
                        obscureText: true,
                        decoration: const InputDecoration(
                          labelText: '确认密码',
                          border: OutlineInputBorder(),
                          isDense: true,
                          contentPadding: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
                        ),
                        style: const TextStyle(fontSize: 15),
                        validator: (value) {
                          if (value == null || value.isEmpty) {
                            return '请再次输入新密码';
                          }
                          if (value != _newPwdController.text) {
                            return '两次输入的新密码不一致';
                          }
                          return null;
                        },
                      ),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 64),
              SizedBox(
                width: double.infinity,
                child: Button(
                  type: ButtonType.primary,
                  onPressed: _isLoading ? null : _submit,
                  child: const Text('确认修改密码'),
                ),
              ),
            ],
          )
        ),
      ),
    );
  }
} 